//
//  TiXianViewController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/7.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "TiXianViewController.h"
#import "TiXianPassWordViewController.h"
#import "MyBankCardController.h"



@interface TiXianViewController ()
{
    NSString *moneyStr;//余额
    NSString *moenyTiXian;//体现的金额
}

@property (nonatomic, strong)UIButton *rightBtn2;//右侧按钮



@end

@implementation TiXianViewController
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self getBanace];
}

- (void)getBanace {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    [HttpRequest getWithURLString:POST(Shop_PersonalGetUserBalance) parameters:parameDic success:^(id responseObject) {
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"获取余额出错"];
            return ;
        }
        NSDictionary *tempD = responseObject[@"data"];
        if (![[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"0"]) {
            [MBProgressHUD showText:tempD[@"msg"]];
            return;
        }

        moneyStr = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"info"][@"balance"]];
         _shengYuMoenyLB.text = [NSString stringWithFormat:@"%@元",moneyStr];
        
    } failure:^(NSError *error) {
        
    }];

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    moenyTiXian = @"";
    [self configureNaBar];
    [self configureV];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeBankTiXain:) name:@"changeBankTiXain" object:nil]; //注：务必让接收者中name后面的参数和发送者中的name后面的参数一样。
    

    
}

- (void)changeBankTiXain:(NSNotification *)notification {
    _bankModel = notification.userInfo[@"BankCard"];
    
    if ([_bankModel.bankname isEqualToString:@"中国农业银行"]) {
        self.bgV.image = [UIImage imageNamed:@"lv"];
        self.logV.image = [UIImage imageNamed:@"nongxin"];
    }
    else if([_bankModel.bankname isEqualToString:@"中国工商银行"]) {
        self.bgV.image = [UIImage imageNamed:@"hong"];
        self.logV.image = [UIImage imageNamed:@"zhaohang"];
    }else {
        self.bgV.image = [UIImage imageNamed:@"lan"];
        self.logV.image = [UIImage imageNamed:@"yinlian"];
    }
    _titlNameLB.text = _bankModel.bankname;
    
    _cardNumLB.text = [self getNewBankNumWitOldBankNum:_bankModel.cardnum];
    
}

- (void)configureNaBar {
        self.navigationItem.title = @"提现";
        //设置导航栏按钮的边距
        UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame= CGRectMake(0, 0, 60, 30);
        [rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
        [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
        /**
         width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
         */
        negativeSpacerleft.width = -20;
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
        
        
    //设置导航栏按钮的边距
    _rightBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn2.frame= CGRectMake(0, 0, 110, 30);
    [_rightBtn2 setTitle:@"更换银行卡" forState:(UIControlStateNormal)];
    [_rightBtn2 addTarget:self action:@selector(onClickRight) forControlEvents:UIControlEventTouchUpInside];
    _rightBtn2.titleLabel.font = kFont16;
    [_rightBtn2 setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn2];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
   
}

//配置视图数据布局
- (void)configureV {
    
    
    if ([_bankModel.bankname isEqualToString:@"中国农业银行"]) {
        self.bgV.image = [UIImage imageNamed:@"lv"];
        self.logV.image = [UIImage imageNamed:@"nongxin"];
    }
    else if([_bankModel.bankname isEqualToString:@"中国工商银行"]) {
        self.bgV.image = [UIImage imageNamed:@"hong"];
        self.logV.image = [UIImage imageNamed:@"zhaohang"];
    }else {
        self.bgV.image = [UIImage imageNamed:@"lan"];
        self.logV.image = [UIImage imageNamed:@"yinlian"];
    }
    _titlNameLB.text = _bankModel.bankname;
    
    _cardNumLB.text = [self getNewBankNumWitOldBankNum:_bankModel.cardnum];
    
   
    
    [_moenyTF addTarget:self action:@selector(handleTextTiXian:) forControlEvents:(UIControlEventEditingChanged)];
    
}
- (void)handleTextTiXian:(UITextField *)text {
    moenyTiXian = text.text;
}
-(NSString *)getNewBankNumWitOldBankNum:(NSString *)bankNum
{
    NSMutableString *mutableStr;
    if (bankNum.length) {
        mutableStr = [NSMutableString stringWithString:bankNum];
        for (int i = 0 ; i < mutableStr.length; i ++) {
            if (i>2&&i<mutableStr.length - 3) {
                [mutableStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
            }
        }
        NSString *text = mutableStr;
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *newString = @"";
        while (text.length > 0) {
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        return newString;
    }
    return bankNum;
    
}
- (void)handleBackAxtion {
        [self.navigationController popViewControllerAnimated:YES];
    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 更换银行卡按钮的点击事件
- (void)onClickRight {

    MyBankCardController *pass = [[MyBankCardController alloc] init];
    pass.typeST = @"tiXian";
    [self.navigationController pushViewController:pass animated:YES];
    
}






#pragma mark 确认提现按钮的点击事件
- (IBAction)sureTiXianBtnACtion:(UIButton *)sender {
    
    
    
    if ([self isBlankString:moenyTiXian] == YES) {
        [MBProgressHUD showText:@"请输入提现金额"];
        return;
    }
    if ([moenyTiXian floatValue] > [moneyStr floatValue]) {
        [MBProgressHUD showText:@"提现金额不能大于余额"];
        return;
    }
    
    
    TiXianPassWordViewController *pass = [[TiXianPassWordViewController alloc] init];
    
    pass.typeStr = _typePerson;//体现的身份
    pass.amountT = moenyTiXian;//提现的金额
    pass.backcard_idT = _bankModel.Id;//提现的银行卡号
    
    [self.navigationController pushViewController:pass animated:YES];
    
    
}
//判断一个字符串是否为空
- (BOOL) isBlankString:(NSString *)string {
    
    if (string == nil || string == NULL) {
        
        return YES;
        
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        
        return YES;
        
    }
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        
        return YES;
        
    }
    
    return NO;
    
}




@end
